package member.model.dao;

import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import member.to.MemberTO;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;

public class MemberDAO {	
	private static MemberDAO instance = new MemberDAO();
	private SqlMapClient sqlMap;
	
	private MemberDAO(){
		try {
			Reader reader = Resources.getResourceAsReader("member/config/SqlMapConfig.xml");
			sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
		} catch (IOException e) {			
			e.printStackTrace();
		}
	}
	public static MemberDAO getInstance(){
		return instance;
	}
	public MemberTO selectMemberById(String id) throws SQLException{
		MemberTO obj = (MemberTO) sqlMap.queryForObject("selectMemberById",id);
		return obj;
	}
	public MemberTO selectMemberByRegisterNumber(String registerNumber1,String registerNumber2) throws SQLException{
		HashMap<String, String> map = new HashMap<String, String>();
		map.put("registerNumber1", registerNumber1);
		map.put("registerNumber2", registerNumber2);
		return (MemberTO)sqlMap.queryForObject("selectMemberByRegisterNumber",map);		
	}
	public MemberTO selectMemberByRegisterNumber(String registerNumber1, String registerNumber2, String id)throws SQLException{
		HashMap<String, String> map = new HashMap<String, String>();
		map.put("id",id);
		map.put("registerNumber1", registerNumber1);
		map.put("registerNumber2", registerNumber2);		
		return (MemberTO)sqlMap.queryForObject("selectMemberByRegisterNumber",map);		
	}
	public void insertMember(MemberTO mto) throws SQLException{
		sqlMap.insert("insertMember",mto);
	} 	
	public ArrayList<MemberTO> selectAllMember()throws SQLException{
		return (ArrayList)sqlMap.queryForList("selectAllMember");
		
	}
	public ArrayList<MemberTO> selectMemberByMileageRange(int sMileage, int eMileage)throws SQLException{
		HashMap<String, Integer> map = new HashMap<String, Integer>();
		map.put("sMileage", sMileage);
		map.put("eMileage", eMileage);
		ArrayList list = (ArrayList) sqlMap.queryForList("selectMemberByMileageRange",map);		
		return list;
	}
	public void updateMember(MemberTO mto)  throws SQLException{
		 sqlMap.update("updateMember",mto);
	}
	public void deleteMemberById(String id)throws SQLException{
		sqlMap.delete("deleteMemberById",id);
	}
	public ArrayList<MemberTO> selectMemberByName(String name) throws SQLException{
		ArrayList list =(ArrayList) sqlMap.queryForList("selectMemberByName",name);		
		return list;
	}
	
	
	}
	
	
		
	
	
	
	
	
	
















